package com.eca.emas.daemon.shared;

import java.sql.Timestamp;
import java.util.ArrayList;

public class LocationChecker {

	public void checkAllLocations() {
		ArrayList<Location> locations = LocationManager.getInstance()
				.getLocations();
		for (Location location : locations) {
			if (checkDataValidityForLocation(location)) {
				EMASMailManager.getInstance().sendErrorMessageToAllRecipients(
						EMASMailManager.ERROR_NO_NEW_VALUES + "\n Standort: "
								+ location.toString());
			}
		}

	}

	@SuppressWarnings("deprecation")
	private boolean checkDataValidityForLocation(Location location) {
		float firstTemp;

		ArrayList<Sensor> sensors = MySQLConnectionManager.getInstance()
				.getSensorsForLocation(location);
		for (Sensor sensor : sensors) {

			Timestamp twoHoursAgo = new Timestamp(System.currentTimeMillis());
			twoHoursAgo.setHours(twoHoursAgo.getHours() - 2);

			ArrayList<SensorData> data = MySQLConnectionManager.getInstance()
					.getSensorDataForLocationIntervallSensor(location,
							twoHoursAgo,
							new Timestamp(System.currentTimeMillis()), sensor);
			if (data != null && !data.isEmpty()) {
				firstTemp = data.get(0).getAvgTemp();
				for (SensorData sensorData : data) {
					if (sensorData.getAvgTemp() != firstTemp) {
						return true;
					}
				}
			}
		}

		return false;
	}

}
